package com.ningxia.water.service.impl;

import com.ningxia.water.entity.TsMeterFlow;
import com.ningxia.water.entity.TsWellWarn;
import com.ningxia.water.enums.WarnCodeEnum;
import com.ningxia.water.enums.WarnStatusEnum;
import com.ningxia.water.enums.WellStatusEnum;
import com.ningxia.water.mapper.TsApplicantWellMapper;
import com.ningxia.water.mapper.TsMeterFlowMapper;
import com.ningxia.water.mapper.TsWellInfoMapper;
import com.ningxia.water.mapper.TsWellWarnMapper;
import com.ningxia.water.service.ITsMeterFlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningxia.water.vo.out.DataDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;

/**
 * 服务实现类
 *
 * @author hz
 * @since 2021-03-15
 */
@Service
public class TsMeterFlowServiceImpl extends ServiceImpl<TsMeterFlowMapper, TsMeterFlow> implements ITsMeterFlowService {

    @Autowired
    private TsMeterFlowMapper meterFlowMapper;

    @Autowired
    private TsWellWarnMapper wellWarnMapper;

    @Autowired
    private TsApplicantWellMapper applicantWellMapper;

    @Autowired
    private TsWellInfoMapper wellInfoMapper;


    @Override
    public Integer checkData(Integer meterId) {
        return meterFlowMapper.checkData(meterId);
    }

    /**
     * 水量异常报警
     *
     * @throws Exception
     */
    @Override
    public void monitorVolumeWarnInfos() throws Exception {
        LocalDateTime now = LocalDateTime.now();
        //获取当前时间
        String timeNow = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //30分钟前
        String timePre = now.minusMinutes(30).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        List<TsWellWarn> list = meterFlowMapper.monitorVolumeWarnInfos(timeNow, timePre);
        ListIterator<TsWellWarn> it = list.listIterator();
        while (it.hasNext()) {
            TsWellWarn wellWarn = it.next();
            wellWarn.setWarnTime(now);
            wellWarn.setStatus(WarnStatusEnum.UNTREATED.getCode());
            wellWarn.setWarnCode(WarnCodeEnum.VOLUME_ABNORMAL.getCode());
            try {
                wellWarnMapper.insert(wellWarn);
                //设置机井状态为4444
                wellInfoMapper.setWellStatusByWellId(wellWarn.getWellId(), WellStatusEnum.MALFUNCTION.getCode());
            } catch (Exception e) {
                throw new Exception("执行错误");
            }
        }
    }

    /**
     * 检测设备故障（机井故障）
     *
     * @return
     */
    @Override
    public void monitorWellWarnInfos() throws Exception {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(new Date().getTime()), ZoneId.systemDefault()).minusDays(1);
        //前一天的00:00:00
        String startTime = localDateTime.with(LocalDateTime.MIN)
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //前一天的23:59:59
        String endTime = localDateTime.with(LocalDateTime.MAX)
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //有记录的wellid
        List<Integer> wellIdsOfRecords = meterFlowMapper.getWellIdOfRecords(startTime, endTime);
        //全部wellId
        List<Integer> wellIds = meterFlowMapper.getAllWellIds();
        //看全部wellId中是否包含有记录的wellid，不包含就加入报警中
        ListIterator<Integer> it = wellIdsOfRecords.listIterator();
        while (it.hasNext()) {
            Integer wellId = it.next();
            if (!wellIds.contains(wellId)) {
                Integer applicantId = applicantWellMapper.selectById(wellId).getApplicantId();
                try {
                    wellWarnMapper.insert(new TsWellWarn(WarnCodeEnum.WELL_MALFUNCTION.getCode(),
                            wellId, WarnStatusEnum.UNTREATED.getCode(), LocalDateTime.now(), applicantId));
                    //设置机井状态为4444
                    wellInfoMapper.setWellStatusByWellId(wellId, WellStatusEnum.MALFUNCTION.getCode());
                } catch (Exception e) {
                    throw new Exception("操作失败");
                }

            }
        }
    }

    /**
     * 电池电量检测
     */
    @Override
    public void monitorMeterInfos() throws Exception {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(
                Instant.ofEpochMilli(new Date().getTime()), ZoneId.systemDefault()).minusDays(1);
        //前一天的00:00:00
        String startTime = localDateTime.with(LocalDateTime.MIN)
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //前一天的23:59:59
        String endTime = localDateTime.with(LocalDateTime.MAX)
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        List<TsWellWarn> list = meterFlowMapper.monitorMeterInfos(startTime, endTime);
        ListIterator<TsWellWarn> it = list.listIterator();
        while (it.hasNext()) {
            TsWellWarn wellWarn = it.next();
            wellWarn.setWarnCode(WarnCodeEnum.BATTERY_POWER.getCode());
            wellWarn.setStatus(WarnStatusEnum.UNTREATED.getCode());
            wellWarn.setWarnTime(LocalDateTime.now());
            try {
                wellWarnMapper.insert(wellWarn);
                //设置机井状态为4444
                wellInfoMapper.setWellStatusByWellId(wellWarn.getWellId(), WellStatusEnum.MALFUNCTION.getCode());
            } catch (Exception e) {
                throw new Exception("执行失败");
            }

        }

    }

    @Override
    public List<DataDetailsVo> getDataDetails(String meterId, String wellName, String applicantName) {
        return meterFlowMapper.getDataDetails(meterId, wellName, applicantName);
    }
}
